<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;

	//后台主页
	class Column extends Allow
	{
    public function getindex()
    {
      //拿到List表全部数据
            $data=Db::query("select *,concat(path,',',id) as paths from list order by paths;");
      foreach($data as $key=>$value){
        $num=count(explode(',',$value['path']))-1;
        $data[$key]['name']=str_repeat('--|',$num).$value['name'];
      }
            //分配数据
      return $this->fetch('column/index',['data'=>$data]);
    }

		public function getAdd()
		{
			//获取ID
			$request=request();
			$id=$request->param('id');
			//查询数据
			$list=Db::table('list')->where('id',$id)->select();
            //分配模板
		    return $this->fetch('column/add',['list'=>$list]);
	    }
	    public function postInsert()
	    {
	    	//遍历数据判断条件
	    	foreach($_POST as $k=>$v)
	    	{
	    		if($v==""){
	    			$this->error('请填写完整',"/admin/list");
	    			exit;
	    		}
	    	}
	    	//获取参数
	    	$request=request();
            //获取path
            $path=$request->param('path');
            //获取PID
            $pid=$request->param('pid');
            //合成新的path
            $newpath=$path.','.$pid;
            //挑选参数(list)
            $data=$request->except(['path','action']);
            //将新的path插入
            $data['path']=$newpath;
            //执行添加到list表
            $list=Db::table('list')->insert($data);
            if($list){
            	$this->success('添加成功','/column/index');
            }else{
            	$this->error('添加失败','/column/index');
            }
          }
       public function getEdit()
       {
       	//获取pid与ID
         $request=request();
         $id=$request->param('id');
         //根据id获取数据
         $list=Db::table('list')->where('id',$id)->find();
         //逗号来分割字符串
        $a=explode(',',$list['path']);
        //移除最后一个元素
        array_pop($a);
        //将数组转化为字符串
        $b=implode(',',$a);
         //获取所有数据父类集
         $data=Db::table('list')->where('id','<>',$id)->where('path',$b)->select();
         //判断是否有子集
         $result=DB::table('list')->where('pid',$id)->select();
         //加载模板
         return $this->fetch('column/edit',['list'=>$list,'data'=>$data,'result'=>$result]);
       }
       public function postupdate()
       {
        $request=request();
        $list=$request->except('action');
        if(!empty($list['pid']))
        {
           foreach( $list as $k=>$v)
            {
                if($v==""){
                    $this->error('请填写完整',"/column/index");
                    exit;
                }
            }
        }else
        {
          $data=$request->except(['action','a']);
           foreach( $data as $k=>$v)
            {
                if($v==""){
                    $this->error('请填写完整',"/column/index");
                    exit;
                }
            }

        }
          //获取ID和父类ID
          $id=$request->param('id');
          //获取参数
          $data=$request->post();
          //判断pid是否存在 不存在即为顶级
          if(!empty($_POST['pid'])){
           $pid=$request->param('pid');
           //获取父类数据
           $list=Db::table('list')->where('id',$pid)->find();
           //最终数据
           $data['path']=$list['path'].','.$list['id'];
           
           }else{
          $data['path']=0;
          $data['pid']=0;
           }
       //执行修改
       $result=Db::table('list')->where('id',$id)->update($data);
       //判断是否成功
       if($result)
       {
            $this->success('修改成功',"/column/index");
      }else{
            $this->error('修改失败',"/column/index");
        }

       }
       public  function getdelete()
       {
        $request=request();
        $id=$request->param('id');
        //判断是否有子栏目
        $data=Db::table('list')->where('pid',$id)->select();
        if(!empty($data))
        {
            $this->error('请先删除该栏目的子栏目','/column/index');
        }else
        {
         $list=Db::table('list')->where('id',$id)->delete();
         if($list)
         {
          $this->success('删除成功','/column/index');
         }else
         {
          $this->error('删除失败','/column/index');
         }
        }
       }
       //顶级添加
       public function getadds()
       {
        return $this->fetch('column/adds');
       }
       public function postinserts()
       {
        //遍历数据判断条件
        foreach($_POST as $k=>$v)
        {
          if($v==""){
            $this->error('请填写完整',"/column/adds");
            exit;
          }
        

        //获取参数
            $request=request();
           
            //挑选参数
            $data=$request->except(['action']);
            //path为0
            $data['path']=0;
            //pid为0
             $data['pid']=0;
            //执行添加
            $list=Db::table('list')->insert($data);
            if($list){
              $this->success('添加成功','/column/index');
            }else{
              $this->error('添加失败','/column/index');
            }
          }
       }
		

	}
 ?>